﻿using System;
using System.Linq;

namespace USERS.CYT369.COM.DAL
{
    public class UserService : USERS.CYT369.COM.DAL.DbContexts.BaseService<UserService>
    {
        public UserService() { }


        public USERS.CYT369.COM.MODELS.SysUser GetUserByID(long userId)
        {
            return base.GetOne<USERS.CYT369.COM.MODELS.SysUser>(c => c.Id == userId);
        }

        public USERS.CYT369.COM.MODELS.SysUser GetUserByID(string userId)
        {
            long id = 0;
            long.TryParse(userId, out id);
            return base.GetOne<USERS.CYT369.COM.MODELS.SysUser>(c => c.Id == id);
        }

        /// <summary>
        /// 按登录名查询用户
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public USERS.CYT369.COM.MODELS.SysUser GetUserByUserName(string userName)
        {
            if (userName != null)
            {
                userName = userName.ToUpper();
            }
            return base.GetOne<USERS.CYT369.COM.MODELS.SysUser>(c => c.NormalizedUserName == userName);
        }

        /// <summary>
        /// 按Email查询用户
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public USERS.CYT369.COM.MODELS.SysUser GetUserByEmail(string email)
        {
            if (email != null)
            {
                email = email.ToUpper();
            }
            return base.GetOne<USERS.CYT369.COM.MODELS.SysUser>(c => c.NormalizedEmail == email);
        }

        /// <summary>
        /// 按电话号码查询用户
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public USERS.CYT369.COM.MODELS.SysUser GetUserByPhoneNumber(string phoneNumber)
        {
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                return base.GetOne<USERS.CYT369.COM.MODELS.SysUser>(c => c.PhoneNumber == phoneNumber);
            }

            return null;
        }

        /// <summary>
        /// 保存用户登录日志记录
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        public bool SaveUserLoginLog(USERS.CYT369.COM.MODELS.UserLoginLog log)
        {
            return base.Save<USERS.CYT369.COM.MODELS.UserLoginLog>(log);
        }


        /// <summary>
        /// 读取用户配置
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IQueryable<USERS.CYT369.COM.MODELS.UserConfig> GetUserConfigs(int userId)
        {
            return base.Get<USERS.CYT369.COM.MODELS.UserConfig>(c=>c.UserID == userId);
        }

        /// <summary>
        /// 读取用户配置
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public USERS.CYT369.COM.MODELS.UserConfig GetUserConfig(int userId, string code)
        {
            return base.GetOne<USERS.CYT369.COM.MODELS.UserConfig>(c => c.UserID == userId && c.Code == code);
        }


    }
}
